package models

import (
	"gorm.io/gorm"
	"time"
	"user_srv/basic/config"
)

type Order struct {
	Id             uint           `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	UserId         uint           `gorm:"column:user_id;type:bigint(20);comment:用户id;not null;" json:"user_id"`                                                                      // 用户id
	ReservationId  uint           `gorm:"column:reservation_id;type:bigint(20);comment:保洁Id;not null;" json:"reservation_id"`                                                        // 保洁Id
	OrderSn        string         `gorm:"column:order_sn;type:varchar(255);comment:订单编号;not null;" json:"order_sn"`                                                                // 订单编号
	ServiceAddress string         `gorm:"column:service_address;type:varchar(30);comment:服务地址;not null;" json:"service_address"`                                                   // 服务地址
	ServiceType    string         `gorm:"column:service_type;type:varchar(30);comment:服务类型;not null;" json:"service_type"`                                                         // 服务类型
	OrderTotal     float64        `gorm:"column:order_total;type:decimal(10, 2);comment:订单总价;not null;" json:"order_total"`                                                        // 订单总价
	OrderStatus    uint           `gorm:"column:order_status;type:tinyint(1) UNSIGNED;comment:订单状态:1:待接单，2：待支付，3:已支付，4：取消支付;not null;default:1;" json:"order_status"` // 订单状态:1:待接单，2：待支付，3:已支付，4：取消支付
	CreatedTime    time.Time      `gorm:"column:created_time;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_time"`
	UpdatedTime    time.Time      `gorm:"column:updated_time;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_time"`
	DeletedAt      gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (o *Order) TableName() string {
	return "order"
}
func (o *Order) CreateOrder() error { //创建订单
	return config.DB.Create(&o).Error
}
func (o *Order) UpdateOrderStatus() error { //修改订单状态
	return config.DB.Model(&Order{}).Where("id = ?", o.Id).Update("order_status", o.OrderStatus).Error
}
func (o *Order) MyOrderList() (orderList []*Order, err error) {
	err = config.DB.Where("user_id = ?", o.UserId).Find(&orderList).Error
	return
}
